<template>
  <div class="commentReply">
    <!-- 导航栏 -->
    <div class="nav_bar">
      <van-nav-bar :title="total_count + '条回复'">
        <van-icon slot="left" name="cross" @click="$emit('close')" />
      </van-nav-bar>
    </div>
    <!-- /导航栏 -->

    <!-- 被回复的人的信息 -->
    <van-cell>
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="commentReply.aut_photo"
      />
      <span style="color: #466b9d;" slot="title">{{ commentReply.aut_name }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ commentReply.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{ commentReply.pubdate | relative }}</span>
          <!-- <van-button size="mini" type="default" class="res_btn">
            {{ commentReply.reply_count }} 回复
          </van-button> -->
        </p>
      </div>
      <van-icon
        slot="right-icon"
        :name="commentReply.is_liking ? 'like' : 'like-o'"
        @click="toggleCommentLike(commentReply)"
      />
    </van-cell>
    <!-- /被回复的人的信息 -->

    <!-- 分割线 -->
    <van-divider>全部回复</van-divider>

    <!-- 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="v in list" :key="v.aut_id.toString()">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="v.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ v.title }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ v.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ v.pubdate | relative }}</span>
            <van-button size="mini" type="default" class="res_btn">
              回复
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="v.is_liking ? 'like' : 'like-o'"
          @click="toggleCommentLike(v)"
        />
      </van-cell>
    </van-list>
    <!-- /回复列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
        @keyup.enter="addComment"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import {
  getCommentsReply,
  postCommentsReply,
  postLikings,
  delLikings,
} from '@/api/comment'
export default {
  name: 'CommentReply',
  props: ['commentReply', 'art_id'],
  data() {
    return {
      list: [], // 搜索结果
      loading: false, // 是否正在加载
      finished: false, // 是否加载完毕
      offset: null,
      content: '', // 发布评论
      show: false,
      total_count: '',
    }
  },
  methods: {
    // 一、加载评论
    async onLoad() {
      // 1.1 发送请求,保存数据
      const { data } = await getCommentsReply(
        this.commentReply.com_id.toString(),
        this.offset
      )
      // console.log(data)
      this.total_count = data.data.total_count
      const arr = data.data.results
      this.list = this.list.concat(arr)
      // 1.2 加载状态结束
      this.loading = false
      // 1.3 数据全部加载完成
      if (arr.length === 0) {
        this.finished = true
      }
      // 1.4 更新offset
      this.offset = data.data.last_id
    },

    // 二、发布评论
    async addComment() {
      // 2.1 非空校验
      if (!this.content) return
      // 2.2 发送请求,更新视图,将评论添加到list的最前面
      const { data } = await postCommentsReply(
        this.commentReply.com_id.toString(),
        this.content,
        this.art_id.toString()
      )
      // console.log(data)
      // 解决发布新评论后无法点赞的问题
      data.data.new_obj.is_liking = false
      this.list.unshift(data.data.new_obj)
      // 2.3 清空内容,提示
      this.content = ''
      this.$toast.success('发布成功')
      // 2.4 发送请求获取最新的评论数量
      this.getCmt()
    },

    // 三、对评论点赞/取消点赞
    async toggleCommentLike(v) {
      // console.log(v)
      try {
        // 3.1 判断当前是否已经点赞 is_liking:false 表示没点赞
        if (!v.is_liking) {
          // 值为false 发送点赞请求
          await postLikings(v.com_id.toString())
          this.$toast.success('点赞')
        } else {
          // 值为true 发送取消点赞请求
          await delLikings(v.com_id.toString())
          this.$toast.success('取消点赞')
        }
        // 3.2 更新视图
        v.is_liking = !v.is_liking
      } catch {
        this.$toast.success('操作失败')
      }
    },

    // 四、获取最新的评论数量
    async getCmt() {
      const { data } = await getCommentsReply(
        this.commentReply.com_id.toString(),
        this.offset
      )
      this.total_count = data.data.total_count
    },
  },
}
</script>

<style lang="scss" scoped>
.commentReply {
  > .van-cell {
    padding-top: 55px;
  }

  // 发表评论的区域是固定在下端的
  .publish-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
  // 给发表评论区空出地方
  .van-list {
    margin-bottom: 45px;
  }
  .res_btn {
    top: -4px;
  }
  .van-icon-like {
    color: red;
  }
  .nav_bar {
    position: fixed;
    z-index: 999;
    width: 100%;
    border-radius: 15px 15px 0 0;
    overflow: hidden;
  }
}
</style>
